package com.hy.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.hy.appbase.dozer.DozerBase;
import com.hy.appbase.exception.BusinessException;
import com.hy.appbase.page.PageResult;
import com.hy.bo.input.GazetteerAddBo;
import com.hy.bo.query.GazetteerQueryBo;
import com.hy.bo.result.GazetteerRetBo;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Path;

import com.hy.bo.result.GazetteerTreeRetBo;
import com.hy.mapper.GazetteerMapper;
import com.hy.mapper.GazetteerMapper_My;
import com.hy.model.Gazetteer;
import com.hy.model.GazetteerExample;
import com.hy.model.GazetteerNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.hy.entity.GazetteerEntity;
import com.hy.service.IGazetteerService;
import org.springframework.util.StringUtils;


@Service
public class GazetteerServiceImpl extends DozerBase implements IGazetteerService {

//	@Autowired
//	private GazetteerJpaRepo gazetteerJpaRepo;

 	@Autowired
 	private GazetteerMapper gazetteerMapper;

 	@Autowired
	private GazetteerMapper_My gazetteerMapper_my;
	
	@Override
	public GazetteerRetBo getGazetteerById(String id) {
		Gazetteer gazetteer = gazetteerMapper.selectByPrimaryKey(id);
		GazetteerRetBo gazetteerRetBo = getDozerMapper().map(gazetteer, GazetteerRetBo.class);
		return gazetteerRetBo;
	}

	@Override
	public GazetteerRetBo saveGazetteer(GazetteerAddBo gazetteerAddBo) {
		GazetteerExample gazetteerExample = new GazetteerExample();

		Gazetteer gazetteer = getDozerMapper().map(gazetteerAddBo, Gazetteer.class);

		gazetteer.setUpdateBy(gazetteerAddBo.getOperatorBy());


		GazetteerRetBo gazetteerRetBo = getDozerMapper().map(gazetteer, GazetteerRetBo.class);
		return gazetteerRetBo;
	}

	@Override
	public void deleteGazetteer(String guid) {
		gazetteerMapper.deleteByPrimaryKey(guid);
	}

	@Override
	public PageResult<GazetteerRetBo> page(GazetteerQueryBo gazetteerQueryBo){
//		Specification<GazetteerEntity> specification =buildSpecification(gazetteerQueryBo);
//		Sort sort = new Sort(Direction.DESC, "createTime");
//		Pageable pageable = new PageRequest(gazetteerQueryBo.getPageIndex(), gazetteerQueryBo.getPageSize(), sort);
//		Page<GazetteerEntity> gazetteerEntities = gazetteerJpaRepo.findAll(specification, pageable);
//		if(gazetteerEntities.getTotalElements() > 0) {
//			List<GazetteerEntity> gazetteerEntitiesContent = gazetteerEntities.getContent();
//			List<GazetteerRetBo> gazetteerRetBos = new ArrayList<GazetteerRetBo>();
//			for (GazetteerEntity gazetteerEntity : gazetteerEntitiesContent) {
//				GazetteerRetBo gazetteerRetBo = new GazetteerRetBo();
//				BeanUtils.copyProperties(gazetteerEntity, gazetteerRetBo);
//				gazetteerRetBos.add(gazetteerRetBo);
//			}
//			return new PageResult<GazetteerRetBo>(gazetteerRetBos, gazetteerEntities.getTotalElements());
//		} else {
//			return new PageResult<GazetteerRetBo>();
//		}
		return null;
	}

	public List<Gazetteer> list(GazetteerQueryBo gazetteerQueryBo){
		GazetteerExample gazetteerExample = new GazetteerExample();
		if(!StringUtils.isEmpty(gazetteerQueryBo.getCode())) {
			gazetteerExample.or().andCodeLike(gazetteerQueryBo.getCode()+"%");
		}
		List<Gazetteer> gazetteers = gazetteerMapper.selectByExample(gazetteerExample);
		return gazetteers;
	}

	@Override
	public GazetteerTreeRetBo findAllGazetteerNode() {
		GazetteerNode gazetteerNode = gazetteerMapper_my.getNodeTree();
		if(gazetteerNode==null) throw new BusinessException("没有数据");
		GazetteerTreeRetBo gazetteerTreeRetBo = this.getDozerMapper().map(gazetteerNode,GazetteerTreeRetBo.class);
		return gazetteerTreeRetBo;
	}

//	private Specification<GazetteerEntity> buildSpecification(GazetteerQueryBo gazetteerQueryBo){
//		return new Specification<GazetteerEntity>() {
//
//			@Override
//			public Predicate toPredicate(Root<GazetteerEntity> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
//				List<Predicate> predicateList = new ArrayList<Predicate>();
//
//				String name = gazetteerQueryBo.getName();
//				if(!StringUtils.isEmpty(name)) {
//					Path<String> $name = root.get("name");
//					Predicate _name = cb.like($name, name);
//					predicateList.add(_name);
//				}
//
//				String code = gazetteerQueryBo.getCode();
//				if(!StringUtils.isEmpty(code)) {
//					Path<String> $code = root.get("code");
//					Predicate _code = cb.equal($code, code);
//					predicateList.add(_code);
//				}
//
//				if(predicateList.isEmpty()) {
//					return cb.conjunction();
//				}
//				Predicate[] predicates = predicateList.toArray(new Predicate[predicateList.size()]);
//				return cb.and(predicates);
//			}
//		};
//	}
}
